package com.meishij.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.meishij.MallCategoryActivity;
import com.meishij.R;
import com.meishij.entry.MallEntry;

import java.util.List;

/**
 * Created by 肖荣 on 2016/10/12.
 */

public class MallCategoryAdapter extends RecyclerView.Adapter<MallCategoryAdapter.ViewHolder> {
    private Context context;
    private List<MallEntry.CategoryInfosBean> list;

    public MallCategoryAdapter(Context context, List<MallEntry.CategoryInfosBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_mall_category_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.mall_category_item_text.setText(list.get(position).getName());
        holder.mall_category_item_image.setImageURI(list.get(position).getIcon());
        View parent = (View) holder.mall_category_item_image.getParent();
        parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String cid1 = list.get(position).getCid1();
                String goodsSource = list.get(position).getGoodsSource();
                String name = list.get(position).getName();
                Intent intent = new Intent(context, MallCategoryActivity.class);
                intent.putExtra("cid1", cid1);
                intent.putExtra("name",name);
                intent.putExtra("goodsSource", goodsSource);
                Log.e("自定义标签", "cid1=" + cid1 + "goodsSource=" + goodsSource);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private SimpleDraweeView mall_category_item_image;
        private TextView mall_category_item_text;

        public ViewHolder(View itemView) {
            super(itemView);
            mall_category_item_image = ((SimpleDraweeView) itemView.findViewById(R.id.mall_category_item_image));
            mall_category_item_text = ((TextView) itemView.findViewById(R.id.mall_category_item_text));
        }
    }
}
